package me.techbird.nve.dao.impl;

import me.techbird.nve.dao.ReportDao;
import me.techbird.nve.domain.ExamTest;
import me.techbird.nve.domain.ExpReport;
import me.techbird.express.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ReportDaoImpl implements ReportDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int addReport(ExpReport report) {
        String sql = "insert into tab_report(uid,process,result,feel,create_time) values(?,?,?,?,?)";
        return template.update(sql, report.getUser().getUid(), report.getProcess(), report.getResult(), report.getFeel(), new Date());
    }


    @Override
    public int findReportCount() {
        String sql = "select count(*) from tab_report";
        Integer count = template.queryForObject(sql, Integer.class);
        return count;
    }

    @Override
    public int findTestCount() {
        String sql = "select count(*) from tab_test";
        Integer count = template.queryForObject(sql, Integer.class);
        return count;
    }

    @Override
    public List<ExpReport> findByPage(int start, int pageSize) {
        String sql = "select * from tab_report where 1=1 ";
        StringBuilder sb = new StringBuilder(sql);
        List<Integer> params = new ArrayList<>();
        sb.append("limit ? , ?");
        sql = sb.toString();
        params.add(start);
        params.add(pageSize);
        System.out.println(sql + params.toString());
        return template.query(sql, new BeanPropertyRowMapper<ExpReport>(ExpReport.class), params.toArray());
    }

    @Override
    public List<ExamTest> findTestByPage(int start, int pageSize) {
        String sql = "select * from tab_test where 1=1 ";
        StringBuilder sb = new StringBuilder(sql);
        List<Integer> params = new ArrayList<>();
        sb.append("limit ? , ?");
        sql = sb.toString();
        params.add(start);
        params.add(pageSize);
        System.out.println(sql + params.toString());
        return template.query(sql, new BeanPropertyRowMapper<ExamTest>(ExamTest.class), params.toArray());
    }
}
